<?php

/**
* AdminSendCodeFormModule.class.php
*
* Concrete page module responsable for creating slide show list
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/

require_once("framework/config.php");
require_once("framework/Auth.php");
require_once("framework/PageModule.class.php");
require_once("HTML/QuickForm.php");
require_once("HTML/QuickForm/Renderer/Default.php");
require_once("PDF.class.php");
require_once('Mail.php');
require_once('Mail/mime.php');


// {{{ SlideShowListModule

class AdminSendCodeFormModule extends PageModule 
{
    // {{{ Class operations 

    // {{{ AdminSendCodeFormModule
    /**
    * C'tor
    */
    
    function AdminSendCodeFormModule()
    {
        trigger_error("ENTER AdminSendCodeFormModule::AdminSendCodeFormModule()", E_USER_NOTICE);

    }
    // }}}
    // {{{ getContent

    /**
    * Function returns page content
    *
    */

    function getContent()
    {

        trigger_error("ENTER AdminSendCodeFormModule::getContent()", E_USER_NOTICE);

        $pdf = new PDF();

        $pdf->setContent($this->getData());

        $pdf->createPage();


        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');

        $path =$upload['upload_dir'];

        if ($path == '')
        {
            trigger_error("AdminSendCodeFormModule::getContent() upload dir not set", E_USER_ERROR);
        }

        $fileName = $path."/".md5(uniqid(rand(), true)).".pdf";

        $pdf->Output($fileName,'F');

        $text = 'Kody w załączniku';

        $file = $fileName;

        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $hdrs = array(
                      'From'    => $global['email'],
                      'Subject' => _('Kody zostały wygenerowane.')
                      );

        $mime = new Mail_mime();

        $mime->setTXTBody($text);
        $mime->addAttachment($file, 'application/pdf');

        $body = $mime->get();
        $hdrs = $mime->headers($hdrs);

        $params = array ( 'host' => $global['host'],
                          'auth'=>true,
                          'username'=>$global['username'],
                          'password'=>$global['password'],
                          'port'=>$global['port']);

        $mail =& Mail::factory('smtp',$params);
        


        if(PEAR::isError($mail))
        {
           trigger_error("AdminSendCodeFormModule::getContent() ".$mail->getMsg(), E_USER_ERROR); 
        }

        $err = $mail->send(AuthSingleton::instance()->getUsername(), $hdrs, $body);

        if(PEAR::isError($err))
        {
           trigger_error("AdminSendCodeFormModule::getContent() ".$err->getMessage(), E_USER_ERROR); 
        }

        if ( !unlink( $fileName ) )
        {
            trigger_error("AdminSendCodeFormModule::getContent() Could not remove file".$fileName, E_USER_ERROR);
        }

        $options = &PEAR::getStaticProperty('CONFIG_INI','TEMPLATES');     
        $statusTpl = new HTML_Template_IT($options["templates_dir"]);
        $statusTpl->loadTemplatefile("status.tpl.xhtml",FALSE,FALSE);

        $statusTpl->setVariable("STATUS",_("Kody wysłane."));
        return $statusTpl->get();
    }
    
    // }}}

    function getData()
    {
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $dbh = new PDO($options['dsn_string']);
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        if( isset($_GET['guide_id']) )
        {
            $sth = $dbh->prepare("SELECT name as title FROM guide WHERE rowid = :id AND owner = :owner");
            $sth->bindParam(':id',$_GET['guide_id'],PDO::PARAM_INT);   
            $sth->bindParam(':owner',  AuthSingleton::instance()->getUsername() ,PDO::PARAM_STR);  
        }
        elseif( isset($_GET['slide_id']) )
        {
            $sth = $dbh->prepare("SELECT guide.name as title, slide.name as subTitle FROM guide,slide WHERE slide.rowid = :id AND guide.rowid = slide.guide_id AND guide.owner = :owner AND slide.owner = :owner");
            $sth->bindParam(':id',$_GET['slide_id'],PDO::PARAM_INT); 
            $sth->bindParam(':owner',  AuthSingleton::instance()->getUsername() ,PDO::PARAM_STR);  
        }
        elseif( isset($_GET['id']) )
        {
            $sth = $dbh->prepare("SELECT guide.rowid as guide_id,guide.name as title FROM guide WHERE guide.rowid = :id AND guide.owner = :owner");
            $sth->bindParam(':id',$_GET['id'],PDO::PARAM_INT);  
            $sth->bindParam(':owner',  AuthSingleton::instance()->getUsername() ,PDO::PARAM_STR);
            $sth->execute();
            
            $result = $sth->fetchAll(PDO::FETCH_ASSOC);
            
            $sth = $dbh->prepare("SELECT guide.rowid as guide_id,slide.rowid as slide_id,guide.name as title, slide.name as subTitle FROM guide,slide WHERE guide.rowid = :id AND slide.guide_id = guide.rowid AND guide.owner = :owner AND slide.owner = :owner");
            $sth->bindParam(':id',$_GET['id'],PDO::PARAM_INT);  
            $sth->bindParam(':owner',  AuthSingleton::instance()->getUsername() ,PDO::PARAM_STR);
            $sth->execute();
            
            $result = array_merge($result, $sth->fetchAll(PDO::FETCH_ASSOC) );
        }
        else    
        {
            return array();
        }


        //Add descryption
        for($i = 0 ; $i < count($result); $i++ )
        {
            $result[$i]['desc'] = _('Pobierz czytnik http://get.neoreader.com');
        }
        return $result;
    }

    // }}}
}

// }}} 

?>
